
//向服务器请求数据 展示到页面
$.ajax({
    type: 'get',
    url: '/comments',
    success: function (response) {
        console.log(response);
        var html = template('commentsTpl', response);
        $('#commentsBox').html(html);
        var htmlpage = template('pageTpl', response);
        $('#pageBox').html(htmlpage);
    }
});

//实现分页功能
function changePage(page) {
    //向服务器请求数据 展示到页面
    $.ajax({
        type: 'get',
        url: '/comments',
        data: {
            page: page
        },
        success: function (response) {
            var html = template('commentsTpl', response);
            $('#commentsBox').html(html);
            var htmlpage = template('pageTpl', response);
            $('#pageBox').html(htmlpage);
        }
    });
}

//使用委托事件添加点击切换状态功能
$('#commentsBox').on('click', '.status', function () {
    var status = $(this).attr('data-status');
    var id = $(this).attr('data-id');
    $.ajax({
        type: 'put',
        url: '/comments/' + id,
        data: {
            state: status == 0 ? 1 : 0
        },
        success: function () {
            location.reload();
        }
    });
})

//使用委托事件添加删除功能
$('#commentsBox').on('click','.delete',function(){
    var id = $(this).attr('data-id');
    $.ajax({
        type:'delete',
        url:'/comments/'+id,
        success:function(){
            location.reload();
        }

    });
})


